﻿var mmenu = {
    //表格初始化
    gridInit: function () {
        $("#mmenu-grid").treegrid({
            url: "/manage/menu/grid",
            nowrap: false,
            rownumbers: true,
            border: false,
            fit: true,
            singleSelect: true,
            idField: "ID",
            toolbar: "#mmenu-tools",
            treeField: "Name",
            parentField: "Pid",
            columns:
            [[
                { field: "Name", title: "名称", width: 200, align: "left" },
                { field: "Type", title: "类型", width: 80, align: "center" },
                { field: "Module", title: "模块", width: 80, align: "center" },
                { field: "Tag", title: "标记", width: 150, align: "center" },
                { field: "Link", title: "连接", width: 250, align: "center" },
                { field: "Sequence", title: "排序", width: 50, align: "center" },
                {
                    field: "IsEnabled", title: "启用", width: 50, align: "center",
                    formatter: function (value, row, index) { return manage.iconYesOrNo(value); }
                }
            ]]
        });
    },

    //添加
    insert: function () {
        var row = $("#mmenu-grid").treegrid("getSelected");
        if (row) {
            mmenu.mutual("添加", null, row.ID);
        }
        else { mmenu.mutual("添加", null, null); }
    },
    //修改
    update: function () {
        var row = $("#mmenu-grid").treegrid("getSelected");
        if (row) {
            mmenu.mutual("修改", row.ID, null);
        }
        else { manage.slide(manage.nullRowMsg); }
    },
    //删除
    remove: function () {
        var row = $("#mmenu-grid").treegrid("getSelected");
        if (row) {
            manage.confirm("确认删除?", function (del) {
                if (del) {
                    manage.ajax({
                        url: "/manage/menu/remove",
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            manage.slide(result.message);
                            if (result.success) { mmenu.reload(); }
                        }
                    });
                }
            });
        }
        else { manage.slide(manage.delNullRowMsg); }
    },
    //实体操作
    mutual: function (title, id, pid) {
        var href = "/manage/menu/page?" + $.param({ id: id, pid: pid });
        var d = manage.dialog({
            title: title, href: href, width: 740,
            handler: function () {
                manage.form({
                    dialog: d, formid: "#mmenu-form",
                    url: "/manage/menu/save",
                    success: function (result) { mmenu.reload(); }
                });
            },
            onLoad: function () { mmenu.formInit(id); }
        });
    },
    //弹窗初始化
    formInit: function (id) {
        $("#mmenu-icon").textbox({
            required: true,
            width: 200,
            icons: [{
                iconCls: "icon-application-view-icons",
                handler: function () {
                    var d = manage.dialog({
                        title: "选择图标",
                        id: "mmenu-iconbox",
                        width: 546, height: 446,
                        iconCls: "icon-bricks",
                        href: "/content/easyui/extensions/icons/select.html",
                        onLoad: function () {
                            $("span.list-span", "#mmenu-iconbox").click(function () {
                                var value = $(this).data("value");
                                d.dialog("close");
                                $("#mmenu-icon").textbox("setValue", value);
                            });
                        }
                    });
                }
            }]
        });

        $("#mmenu-pid").combotree({
            required: true,
            width: 180,
            editable: false,
            url: "/manage/menu/tree",
            onLoadSuccess: function (node, data) {
                if (id) {
                    var etree = $("#mmenu-pid").combotree("tree");
                    etree.tree("remove", etree.tree("find", id).target);
                }
            }
        });
    },

    //排序上移
    desc: function () {
        mmenu.order("desc");
    },
    //排序下移
    asc: function () {
        mmenu.order("asc");
    },
    //排序操作
    order: function (order) {
        var row = $("#mmenu-grid").treegrid("getSelected");
        if (row) {
            manage.ajax({
                url: "/manage/menu/setorder",
                type: "post", data: { id: row.ID, order: order, pid: row.Pid },
                success: function (result) {
                    manage.slide(result.message);
                    if (result.success) {
                        $("#mmenu-grid").treegrid("reload");
                    }
                }
            });
        }
        else { manage.slide(manage.nullRowMsg); }
    },

    //启用
    enable: function () {
        mmenu.enableOrDisable(true);
    },
    //禁用
    disable: function () {
        mmenu.enableOrDisable(false);
    },
    //启用禁用
    enableOrDisable: function (bool) {
        var row = $("#mmenu-grid").treegrid("getSelected");
        if (row) {
            manage.ajax({
                url: "/manage/menu/enableordisable",
                type: "post", data: { id: row.ID, bit: bool },
                success: function (result) {
                    manage.slide(result.message);
                    if (result.success) {
                        $("#mmenu-grid").treegrid("reload");
                    }
                }
            });
        }
        else { manage.slide(manage.nullRowMsg); }
    },

    //刷新列表
    reload: function () {
        $("#mmenu-grid").treegrid("unselectAll");
        $("#mmenu-grid").treegrid("reload");
    },


    //功能分配操作
    assign: function () {
        var row = $("#mmenu-grid").treegrid("getSelected");
        if (row) {
            var d = manage.dialog({
                title: "配置功能",
                width: 732, height: 388,
                href: "/manage/menu/assign",
                handler: function () {
                    var rows = $("#mmenu-menufunction-grid").datagrid("getRows");
                    manage.ajax({
                        url: "/manage/menu/setassign", type: "post",
                        data: { id: row.ID, rows: JSON.stringify(rows) },
                        success: function (result) {
                            if (result.success) {
                                d.dialog("close");
                                manage.slide(result.message);
                            }
                            else { manage.msg(result.message); }
                        }
                    });
                },
                onLoad: function () { mmenu.assignInit(row.ID); }
            });
        }
        else { manage.slide(manage.nullRowMsg); }
    },
    //功能分配初始化
    assignInit: function (id) {
        $("#mmenu-functiongrid").datagrid({
            url: "/manage/function/allgrid",
            title: "全部功能（双击添加）",
            nowrap: false,
            rownumbers: true,
            idField: "ID",
            fit: true,
            border: false,
            singleSelect: true,
            columns: [[
                {
                    field: "Icon", title: "图标", width: 50, align: "center",
                    formatter: function (value, row, index) {
                        if (value) { return manage.icon(value); }
                    }
                },
                { field: "Name", title: "名称", width: 80, align: "center" },
                { field: "Type", title: "类型", width: 80, align: "center" },
                { field: "Remark", title: "描述", width: 280, align: "center" }
            ]],
            onDblClickRow: mmenu.assignAdded
        });
        $("#mmenu-menufunction-grid").datagrid({
            url: "/manage/function/allgridbymenu",
            loadMsg: "Loading...",
            queryParams: { menuid: id },
            title: "已选功能（双击删除）",
            nowrap: false,
            rownumbers: true,
            idField: "ID",
            fit: true,
            border: false,
            singleSelect: true,
            toolbar: "#mmenu-menufunction-tools",
            columns: [[
                {
                    field: "Icon", title: "图标", width: 50, align: "center",
                    formatter: function (value, row, index) { return manage.icon(value); }
                },
                {
                    field: "Name", title: "名称", width: 80, align: "center"
                }
            ]],
            onDblClickRow: mmenu.assignRemove
        });
        $("#mmenu-menufunction-menuid").val(id);
    },
    //功能添加
    assignAdded: function (index, row) {
        if (row) {
            var rows = $("#mmenu-menufunction-grid").datagrid("getRows");
            for (var i = 0; i < rows.length; i++) {
                if (row.ID == rows[i].ID) { return false; }
            }
            $("#mmenu-menufunction-grid").datagrid("appendRow", {
                ID: row.ID, Icon: row.Icon, Name: row.Name
            });
        }
    },
    //功能删除
    assignRemove: function (index, row) {
        if (row) {
            $("#mmenu-menufunction-grid").datagrid("deleteRow", index);
        }
    },
    //功能排序
    assignOrder: function (order) {
        var row = $("#mmenu-menufunction-grid").datagrid("getSelected");
        if (row) {
            var rowindex = $("#mmenu-menufunction-grid").datagrid("getRowIndex", row);
            var rows = $("#mmenu-menufunction-grid").datagrid("getRows");
            if (order == "up") {
                if (rowindex > 0) {
                    var newindex = rowindex - 1;

                    var targetRow = rows[newindex];
                    var currentRow = rows[rowindex];

                    rows[newindex] = currentRow;
                    rows[rowindex] = targetRow;

                    $("#mmenu-menufunction-grid").datagrid("loadData", rows);
                    $("#mmenu-menufunction-grid").datagrid("selectRow", newindex);
                }
                else { manage.slide("到顶啦!"); }
            }
            else {
                if (rowindex < rows.length - 1) {
                    var newindex = rowindex + 1;

                    var targetRow = rows[newindex];
                    var currentRow = rows[rowindex];

                    rows[newindex] = currentRow;
                    rows[rowindex] = targetRow;

                    $("#mmenu-menufunction-grid").datagrid("loadData", rows);
                    $("#mmenu-menufunction-grid").datagrid("selectRow", newindex);
                }
                else { manage.slide("到底啦!"); }
            }
        }
        else { manage.slide(manage.nullRowMsg); }
    },
    //功能清除
    assignClear: function () {
        $("#mmenu-menufunction-grid").datagrid("unselectAll");
        $("#mmenu-menufunction-grid").datagrid("loadData", []);
    }
};

$(function () { mmenu.gridInit(); });